//
//  ModifyAddressViewController.m
//  NOAHWM
//
//  Created by Frank on 6/9/13.
//  Copyright (c) 2013 Ryan. All rights reserved.
//

#import "ModifyAddressViewController.h"
#import "NSString+SBJSON.h"

@interface ModifyAddressViewController ()

@end

@implementation ModifyAddressViewController

@synthesize addressButton;
@synthesize addressPicker;
@synthesize provinceCities;
@synthesize provinces;
@synthesize cities;
@synthesize pickerContainView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNaviTitle: @"地址"];
    [self setLeftNaviItemWithTitle:@"返回" imageName:@"Icon_Back"];
    [self setRightNaviItemWithTitle:@"保存" imageName:@"Icon_keep"];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Provinces_Cities" ofType:@"txt"];
    NSString *proStr = [[[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil] autorelease];
    NSDictionary *dict = [proStr JSONValue];
    self.provinceCities = dict;
    self.addressPicker.delegate = self;
    CGRect rectPickView = self.pickerContainView.frame;
    rectPickView.origin.y = self.view.frame.size.height;
    self.pickerContainView.frame = rectPickView;
}

- (void)viewDidUnload{
    [super viewDidUnload];
    self.addressButton = nil;
    self.addressPicker = nil;
    self.pickerContainView = nil;
    self.cities = nil;
    self.provinceCities = nil;
    self.provinces = nil;
}

- (void)dealloc{
    [addressPicker release];
    [addressButton release];
    [provinceCities release];
    [provinces release];
    [cities release];
    [pickerContainView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    if (self.isViewLoaded && !self.view.window) {
        self.addressPicker = nil;
        self.addressButton = nil;
        self.provinceCities = nil;
        self.provinces = nil;
        self.cities = nil;
        self.pickerContainView = nil;
    }
    // Dispose of any resources that can be recreated.
}

#pragma mark - BaseViewController methods
- (void)leftItemTapped
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark Picker Date Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == kProvinceComponent) {
        return [[self.provinceCities allKeys] count];
    }
    NSInteger row = [pickerView selectedRowInComponent:0];
    NSString *key = [[self.provinceCities allKeys] objectAtIndex:row];
    NSArray *values = [self.provinceCities objectForKey:key];
    return [values count];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *retval = (id)view;
    if (!retval) {
        retval= [[[UILabel alloc] initWithFrame:CGRectMake(5.f, 0.0f, [pickerView rowSizeForComponent:component].width-10.f, [pickerView rowSizeForComponent:component].height)] autorelease];
        retval.backgroundColor = [UIColor clearColor];
        retval.font = [UIFont boldSystemFontOfSize:16.f];
        retval.numberOfLines = 0;
        //        retval.adjustsFontSizeToFitWidth = YES;
        //        if([[UIDevice currentDevice] systemVersion].floatValue < 6.0)
        //            retval.minimumFontSize = 12.f;
        //        else
        //            retval.minimumScaleFactor = 0.5;
    }
    
    NSString *valueString = nil;
    if (component == kProvinceComponent) {
        valueString = [self.provinceCities.allKeys objectAtIndex:row];
        retval.text = valueString;
        return retval;
    }
    NSInteger selectedRow = [pickerView selectedRowInComponent:0];
    NSString *key = [self.provinceCities.allKeys objectAtIndex:selectedRow];
    NSArray *values = [self.provinceCities objectForKey:key];
    /*
     当右边component滚动的时候选择左边的component，row reuse for右边的时候左边对应的valueArray已经改变，
     可能会产生数组越界访问，导致crash
     */
    if(values.count > row)
        valueString = [values objectAtIndex:row];
    else
        valueString = @"";
    
    retval.text = valueString;
    return retval;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == kProvinceComponent) {
        [pickerView reloadComponent:1];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == kCityComponent) {
        return 150;
    }
    return 140;
}

- (IBAction)addressButtonPressed:(id)sender {
    [UIView animateWithDuration:0.3f animations:^(void){
        CGRect rect = self.pickerContainView.frame;
        rect.origin.y = self.view.frame.size.height - rect.size.height;
        self.pickerContainView.frame = rect;
    }];
}

- (IBAction)buttonCancelPressed:(id)sender {
    
    [UIView animateWithDuration:0.3f animations:^(void){
        
        CGRect rect = self.pickerContainView.frame;
        rect.origin.y = self.view.frame.size.height;
        self.pickerContainView.frame = rect;}];
}

- (IBAction)buttonDonePressed:(id)sender {
    NSInteger provinceRow = [self.addressPicker selectedRowInComponent:kProvinceComponent];
    NSInteger cityRow = [self.addressPicker selectedRowInComponent:kCityComponent];
    
    NSString *province = [self.provinceCities.allKeys objectAtIndex:provinceRow];
    NSString *city = [[self.provinceCities objectForKey:province] objectAtIndex:cityRow];
    
    [self.addressButton setTitle:[NSString stringWithFormat:@"%@ %@",province,city] forState:UIControlStateNormal];
    [self.addressButton setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
    
    [UIView animateWithDuration:0.3f animations:^(void){
        CGRect rect = self.pickerContainView.frame;
        rect.origin.y = self.view.frame.size.height;
        self.pickerContainView.frame = rect;}];

}
@end
